package cn.sdhuijin.healthexam.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.RadioGroup;
import cn.sdhuijin.healthexam.DatabaseHelper;
import cn.sdhuijin.healthexam.ExitApplication;
import cn.sdhuijin.healthexam.R;
import cn.sdhuijin.healthexam.control.AbstractControlActivity;
import cn.sdhuijin.healthexam.util.Constants;

/**
 * 健康体检录入-第二页
 * 
 * @author houyuchun
 * 
 */
public class HealthExamTwelvthActivity extends AbstractControlActivity
		implements OnClickListener {

	private String createDate;
	private int[] checkWxyskzIds = { R.id.check_weixianyskz_jieyan,
			R.id.check_weixianyskz_yinjiu, R.id.check_weixianyskz_yinshi,
			R.id.check_weixianyskz_duanlian,
			R.id.check_weixianyskz_jiantizhong, R.id.check_weixianyskz_yimiao };
	private RadioGroup jiangkangpingjia;
	private DatabaseHelper databaseHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_health_exam_twelvth);
		ExitApplication.getInstance().addActivity(this);

		Intent intent = getIntent();
		databaseHelper = new DatabaseHelper(this);
		Bundle bundle = intent.getBundleExtra("bundle");
		if (bundle != null) {
			createDate = bundle.getString("createDate");
		}
		setCurrentEditUserName(createDate);
		addEditTextView(R.id.edt_yufangjiezhong1, "预防接种名称1");
		addEditTextView(R.id.edt_jiezhongriqi1, "预防接种日期1");
		addEditTextView(R.id.edt_jiezhongjigou1, "预防接种机构1");

		addEditTextView(R.id.edt_yufangjiezhong2, "预防接种名称2");
		addEditTextView(R.id.edt_jiezhongriqi2, "预防接种日期2");
		addEditTextView(R.id.edt_jiezhongjigou2, "预防接种机构2");

		addRadioGroupView(R.id.rad_jiangkangpingjia, "健康评价");
		addEditTextView(R.id.edt_jiangkangpingjia_yc1, "健康评价-异常1");
		addEditTextView(R.id.edt_jiangkangpingjia_yc2, "健康评价-异常2");
		addEditTextView(R.id.edt_jiangkangpingjia_yc3, "健康评价-异常3");
		addEditTextView(R.id.edt_jiangkangpingjia_yc4, "健康评价-异常4");

		addRadioGroupView(R.id.rad_jiangkangzhidao, "健康指导");

		addCheckBoxView(checkWxyskzIds, "危险因素控制");
		addEditTextView(R.id.edt_weixianyskz_qita, "危险因素控制-其他");

		jiangkangpingjia = (RadioGroup) findViewById(R.id.rad_jiangkangpingjia);
		jiangkangpingjia
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						if (checkedId == R.id.rad_jiangkangpingjia_yc) {
							((EditText) findViewById(R.id.edt_jiangkangpingjia_yc1))
									.setEnabled(true);
							((EditText) findViewById(R.id.edt_jiangkangpingjia_yc2))
									.setEnabled(true);
							((EditText) findViewById(R.id.edt_jiangkangpingjia_yc3))
									.setEnabled(true);
							((EditText) findViewById(R.id.edt_jiangkangpingjia_yc4))
									.setEnabled(true);
						} else {
							((EditText) findViewById(R.id.edt_jiangkangpingjia_yc1))
									.setEnabled(false);
							((EditText) findViewById(R.id.edt_jiangkangpingjia_yc1))
									.setText("");
							((EditText) findViewById(R.id.edt_jiangkangpingjia_yc2))
									.setEnabled(false);
							((EditText) findViewById(R.id.edt_jiangkangpingjia_yc2))
									.setText("");
							((EditText) findViewById(R.id.edt_jiangkangpingjia_yc3))
									.setEnabled(false);
							((EditText) findViewById(R.id.edt_jiangkangpingjia_yc3))
									.setText("");
							((EditText) findViewById(R.id.edt_jiangkangpingjia_yc4))
									.setEnabled(false);
							((EditText) findViewById(R.id.edt_jiangkangpingjia_yc4))
									.setText("");
						}
					}
				});

		// Properties文件路径
		controlManager.setPropertiesPath(Constants.PROPERTIES_PATH + createDate
				+ ".txt");
		controlManager.toControl();

		setEditTextListener(R.id.edt_jiezhongriqi1);
		setEditTextListener(R.id.edt_jiezhongriqi2);
		this.setButtonListener(this, R.id.btn_previous, R.id.btn_complete);
	}

	/**
	 * 按钮点击处理事件
	 */
	@Override
	public void onClick(View arg0) {
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		// 获取点击按钮
		int id = (Integer) arg0.getId();
		switch (id) {
		case R.id.btn_previous:
			controlManager.toProperties();
			bundle.putString("createDate", createDate);
			// 创建日期
			intent.putExtra("bundle", bundle);
			intent.setClass(this, HealthExamEleventhActivity.class);
			startActivity(intent);
			this.finish();
			break;
		case R.id.btn_complete:
			controlManager.toProperties();
			// 更新索引表
			databaseHelper.updateIndexTable(createDate);
			this.toastInformation(R.string.toast_submit_info);
			intent.setClass(this, IndexActivity.class);
			startActivity(intent);
			this.finish();
			break;
		default:
			break;
		}
	}

}
